#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common/config.h"
#include "userLog.h"

static char *_Config_SpaceTrim(char *pbyStr)
{
	char *pS = pbyStr;
	int i;

	if (pbyStr == NULL)
	{
		return NULL;
	}

	i = 0;
	while (pS[i] == ' ' || pS[i] == '\t')
	{
		i++;
	}

	return (char *)&pS[i];
}

static int _Config_RecoveryDefault(char *pbyFile)
{
	char cmd[256];
	char *pFile = NULL;

	if (pbyFile == NULL)
	{
		return -1;
	}

	unlink(pbyFile);

	pFile = strrchr(pbyFile, '/');
	if (!pFile)
	{
		LOG_ERROR("file:%s error", pbyFile);
		return -1;
	}
	pFile += 1;

	memset(cmd, 0, sizeof(cmd));
	sprintf(cmd, "cp %s/%s %s/ -rf", T_CONFIG_DEFAULT_CFG_ROOT_PATH, pFile, T_CONFIG_ROOT_PATH);
	system(cmd);

	return 0;
}
int Config_LoadFile(char *pbyFile, CONFIG_RECORD_S *pstRecords, int nRecordsNum)
{
	char abyFileInLine[CONFIG_MAX_LINE_IN_FILE][CONFIG_MAX_BYTE_PER_LINE];
	FILE *fp = NULL;
	char abyLine[1024];
	char *pchS = NULL;
	char abyKey[128];
	char abyValue[256];
	int i, j;
	int index = 0, TotalLine = 0;
	int nFoundFlag = 0;

	if (pbyFile == NULL || pstRecords == NULL)
	{
		return -1;
	}

	fp = fopen(pbyFile, "r");
	if (fp == NULL)
	{
		_Config_RecoveryDefault(pbyFile);
	}

	fp = fopen(pbyFile, "r");
	if (fp == NULL)
	{
		LOG_ERROR("error:: can not open file:%s", pbyFile);
		return -1;
	}

	while (fgets(abyLine, sizeof(abyLine), fp))
	{
		// skip head space
		pchS = abyLine;
		while (pchS == ' ')
		{
			pchS += 1;
		}
		// skip comment line
		if (pchS == '#')
		{
			continue;
		}

		strcpy(abyFileInLine[index], pchS);
		index++;
		if (index >= CONFIG_MAX_LINE_IN_FILE)
		{
			break;
		}
	}
	TotalLine = index;

	LOG_INFO("file:%s,total line:%d", pbyFile, TotalLine);

	for (i = 0; i < nRecordsNum; i++)
	{
		nFoundFlag = 0;
		for (j = 0; j < TotalLine; j++)
		{
			if (sscanf(abyFileInLine[j], "%[^=] = %[^\n\r]", abyKey, abyValue) == 2)
			{
				//LOG_INFO("key:%s,value:%s", abyKey, abyValue);
				if (strncmp(pstRecords[i].pbyKey, abyKey, strlen(pstRecords[i].pbyKey)) == 0)
				{
					nFoundFlag = 1;
					if (pstRecords[i].eType == CONFIG_TYPE_INT32)
					{
						*((int *)pstRecords[i].pvValue) = strtol(abyValue, NULL, 0);
						break;
					}
					else if (pstRecords[i].eType == CONFIG_TYPE_UINT32)
					{
						*((unsigned int *)pstRecords[i].pvValue) = strtoul(abyValue, NULL, 0);
						break;
					}
					else if (pstRecords[i].eType == CONFIG_TYPE_INT16)
					{
						*((short *)pstRecords[i].pvValue) = (short)strtol(abyValue, NULL, 0);
						break;
					}
					else if (pstRecords[i].eType == CONFIG_TYPE_UINT16)
					{
						*((unsigned short *)pstRecords[i].pvValue) = (unsigned short)strtol(abyValue, NULL, 0);
						break;
					}
					else if (pstRecords[i].eType == CONFIG_TYPE_INT8)
					{
						*((char *)pstRecords[i].pvValue) = (char)strtol(abyValue, NULL, 0);
						break;
					}
					else if (pstRecords[i].eType == CONFIG_TYPE_UINT8)
					{
						*((unsigned char *)pstRecords[i].pvValue) = (unsigned char)strtol(abyValue, NULL, 0);
						break;
					}
					else if (pstRecords[i].eType == CONFIG_TYPE_FLOAT)
					{
						*((float *)pstRecords[i].pvValue) = strtof(abyValue, NULL);
						break;
					}
					else if (pstRecords[i].eType == CONFIG_TYPE_DOUBLE)
					{
						*((double *)pstRecords[i].pvValue) = strtod(abyValue, NULL);
						break;
					}
					else if (pstRecords[i].eType == CONFIG_TYPE_STRING)
					{
						strcpy((char *)pstRecords[i].pvValue, abyValue);
						break;
					}
					else
					{
						LOG_ERROR("unkown type for key:%s,type:%d", pstRecords[i].pbyKey, pstRecords[i].eType);
						break;
					}
				}
			}
		}
		if (nFoundFlag == 0)
		{
			LOG_ERROR("can not found key:%s", pstRecords[i].pbyKey);
		}
	}

	if (fp)
	{
		fclose(fp);
	}
	return 0;
_error:

	if (fp)
	{
		fclose(fp);
	}
	return -1;
}

int Config_PutFile(char *pbyFile, CONFIG_RECORD_S *pstRecords, int nRecordsNum)
{
	FILE *fp = NULL;
	int i;

	fp = fopen(pbyFile, "w");
	if (fp == NULL)
	{
		return -1;
	}

	for (i = 0; i < nRecordsNum; i++)
	{
		if (pstRecords[i].eType == CONFIG_TYPE_INT32)
		{
			fprintf(fp, "%s=%d\n", pstRecords[i].pbyKey, *((int *)pstRecords[i].pvValue));
		}
		else if (pstRecords[i].eType == CONFIG_TYPE_INT16)
		{
			fprintf(fp, "%s=%d\n", pstRecords[i].pbyKey, *((short *)pstRecords[i].pvValue));
		}
		else if (pstRecords[i].eType == CONFIG_TYPE_INT8)
		{
			fprintf(fp, "%s=%d\n", pstRecords[i].pbyKey, *((char *)pstRecords[i].pvValue));
		}
		else if (pstRecords[i].eType == CONFIG_TYPE_UINT32)
		{
			fprintf(fp, "%s=%u\n", pstRecords[i].pbyKey, *((unsigned int *)pstRecords[i].pvValue));
		}
		else if (pstRecords[i].eType == CONFIG_TYPE_UINT16)
		{
			fprintf(fp, "%s=%u\n", pstRecords[i].pbyKey, *((unsigned short *)pstRecords[i].pvValue));
		}
		else if (pstRecords[i].eType == CONFIG_TYPE_UINT8)
		{
			fprintf(fp, "%s=%u\n", pstRecords[i].pbyKey, *((unsigned char *)pstRecords[i].pvValue));
		}
		else if (pstRecords[i].eType == CONFIG_TYPE_FLOAT || pstRecords[i].eType == CONFIG_TYPE_DOUBLE)
		{
			fprintf(fp, "%s=%f\n", pstRecords[i].pbyKey, *((float *)pstRecords[i].pvValue));
		}
		else if (pstRecords[i].eType == CONFIG_TYPE_STRING)
		{
			fprintf(fp, "%s=%s\n", pstRecords[i].pbyKey, pstRecords[i].pvValue);
		}
	}

	if (fp)
	{
		fclose(fp);
	}
	return 0;

_error:
	if (fp)
	{
		fclose(fp);
	}
	return -1;
}

#if 0

int nValueInt = 0;
char s8Valueint8 = 0;
float fValue = 0.0;
char abyString[128];
CONFIG_RECORD_S astRecords[] =
{
	{"iamint", &nValueInt, CONFIG_TYPE_INT32},
	{"iamint8", &s8Valueint8, CONFIG_TYPE_INT8},
	{"iamfloat", &fValue, CONFIG_TYPE_FLOAT},
	{"iamstring", abyString, CONFIG_TYPE_STRING}
};

#define ARRY_SIZE(a) (sizeof(a) / sizeof(a[0]))
int main()
{
	int i;
	Config_LoadFile("./test.cfg", astRecords, ARRY_SIZE(astRecords));
	LOG_INFO("iamint:%d,iamint8:%d,iamstring:%s", nValueInt, s8Valueint8, abyString);
	for(i=0; i<10; i++)
	{
		LOG_INFO("%c", abyString[i]);
	}

	Config_PutFile("./testout.cfg", astRecords, ARRY_SIZE(astRecords));
}

#endif
